<html>
<head>
<title>Selekcija brokera</title>
</head>
<body>
<H1>Paginacija brokera - sopstveni PHP kod</H1>
<?php
/*
CREATE TABLE `brokeri` (
  `IDb` varchar(5) default NULL,
  `ime_b` varchar(10) default NULL,
  `prezime_b` varchar(20) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='podatci o brokerima';

-- Dumping data for table `brokeri`

INSERT INTO `brokeri` (`IDb`, `ime_b`, `prezime_b`) VALUES 
('aa', 'zlatko', 'ivic'),
('ab', 'janko', 'jankovic'),
-- ad at least 20 rows
('dc', 'Hon', 'Linkom'),
('dd', 'dino', 'lindertrop'),
('de', 'jakob', 'frost');
*/
$connect = mysql_connect("localhost", "root", "") or die("neispravna konekcija");
mysql_select_db("") or die("neispravna baza podataka");
$per_page=6; // broj redova po strani 
$str=$_GET['str'];
// prevencija SQL injection napada
$str = stripslashes($str);
$str = mysql_real_escape_string($str);
if ($str=="") { $str = 1; } else { $str = $_GET['str']; } 
$off=($str-1)*$per_page;
$query = "select * from brokeri";
$results = mysql_query($query) or die(mysql_error());
$nrows = mysql_num_rows($results);
$rem=$nrows%$per_page;
if ($rem==0) {
    $numpa = $nrows/$per_page;
} else {
    $numpa = floor($nrows/$per_page)+1;
}
// pocinje izrada tabele
echo "<table border='2'>\n";
echo "<tr><td colspan=4> <center>Spisak brokera</center></td></tr>\n";
echo "<tr>	<td><b>Sifra brokera</b></td> 	<td><b>Ime</b></td>  <td><b>Prezime</b></td> 	</tr>\n ";
// prikupljaju se podatci za punjenje tabele
$query2 = "select * from brokeri limit $off,$per_page";
$results2 = mysql_query($query2) or die(mysql_error());
$nrows2 = mysql_num_rows($results2);
for ($i=0;$i<$nrows2;$i++)   {
	$row = mysql_fetch_assoc($results2);
	extract($row);
	echo "
		<tr> 
			<td>$IDb</td> 
			<td>$ime_b</td>  
			<td>$prezime_b</td>
		</tr>\n ";
}
echo "<tr>
		<td colspan='3'>";   
		for ($j=1;$j<$numpa+1;$j++)   { 
			if ($j!=$str)	{ echo " <a href='index.php?str=$j'>$j</a> "; }
			else { echo $j;}
		}
echo"</td>
	</tr>\n
     </table>\n";
?>

</body>
</html>
